package org.apache.struts.ehr.medical_records.action;

import java.util.Map;

import org.apache.struts.ehr.medical_records.model.HL7Message;
import org.apache.struts.ehr.medical_records.model.Query;
import org.apache.struts.ehr.medical_records.model.ReplyMessage;
import org.apache.struts.ehr.medical_records.model.TransferToAnotherHospital;

import com.earogya.communicate.Communicator;
import com.earogya.logic.medical_records.CreateADTA02;
import com.earogya.logic.medical_records.ParseReply;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TransferToAnotherHospitalAction extends ActionSupport{

	private TransferToAnotherHospital transferToAnotherHospital;
	private HL7Message message;
	private ReplyMessage replyMessage;
	
	public ReplyMessage getReplyMessage() {
		return replyMessage;
	}

	public HL7Message getMessage() {
		return message;
	}

	public TransferToAnotherHospital getTransferToAnotherHospital() {
		return transferToAnotherHospital;
	}

	public void setTransferToAnotherHospital(TransferToAnotherHospital transferToAnotherHospital) {
		this.transferToAnotherHospital = transferToAnotherHospital;
	}
	
	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		replyMessage=new ReplyMessage();
		replyMessage.setMessage("empty");
		Query query=new Query();
		CreateADTA02 createMessage=new CreateADTA02(transferToAnotherHospital);
		String msg=createMessage.createMessage("CSTH", "hospital");
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			if(!reply.equals("No Reply")){
				ParseReply parse=new ParseReply();
				replyMessage=(ReplyMessage)parse.parseMessage(reply);
			}
		}
		else{
			message.setMessage("empty");
		}
		return SUCCESS;
	}
	
	public void validate(){
		if(transferToAnotherHospital.getPatientId()==0){
			addFieldError("transferToAnotherHospital.patientId", "This field is required");
		}
		if(transferToAnotherHospital.getFromBedNo().length()==0){
			addFieldError("transferToAnotherHospital.fromBedNo", "This field is required");
		}
		if(transferToAnotherHospital.getFromHospital().length()==0){
			addFieldError("transferToAnotherHospital.fromHospital", "This field is required");
		}
		if(transferToAnotherHospital.getFromWardNo().length()==0){
			addFieldError("transferToAnotherHospital.fromWardNo", "This field is required");
		}
		if(transferToAnotherHospital.getReason().length()==0){
			addFieldError("transferToAnotherHospital.reason", "This field is required");
		}
		if(transferToAnotherHospital.getToHospital().length()==0){
			addFieldError("transferToAnotherHospital.toHospital", "This field is required");
		}
		if(transferToAnotherHospital.getTransferringOfficerId().length()==0){
			addFieldError("transferToAnotherHospital.transferringOfficerId", "This field is required");
		}
		if(transferToAnotherHospital.getTransferTime()==null){
			addFieldError("transferToAnotherHospital.transferTime", "This field is required");
		}
	}
}
